//
//  MenuCollectionViewCell.m
//  MKUI
//
//  Created by 苏尚进 on 2020/8/29.
//Copyright © 2020 moon. All rights reserved.
//

#import "MenuCollectionViewCell.h"

@interface MenuCollectionViewCell ()

@end

@implementation MenuCollectionViewCell
- (id)initWithFrame:(CGRect)frame{
	if (self=[super initWithFrame:frame]) {
        self.menuButton = [[MKUIFlowLayoutButton alloc] initWithContentStyle:(MKUIFlowLayoutButtonContentStyleVertical)];
        self.menuButton.imageSize = CGSizeMake(30, 30);
        self.menuButton.interitemSpacing = 10;
        [self.menuButton setTitleColor:[UIColor systemBlueColor] forState:UIControlStateNormal];
        [self.menuButton setTitleColor:[UIColor systemGrayColor] forState:UIControlStateHighlighted];
        self.menuButton.titleLabel.font = [UIFont systemFontOfSize:12];
        self.menuButton.titleLabel.numberOfLines = 2;
        self.menuButton.contentInsets = UIEdgeInsetsMake(5, 5, 5, 5);
        [self.menuButton addTarget:self action:@selector(__buttonDidTap:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.menuButton];
        self.contentView.clipsToBounds = YES;
        self.layer.borderColor = [UIColor grayColor].CGColor;
        self.layer.borderWidth = 1;
	}
	return self;
}
- (void)layoutSubviews{
	[super layoutSubviews];
	CGRect bounds = self.contentView.bounds;
    self.menuButton.frame = bounds;
}
- (void)__buttonDidTap:(id)sender{
    [self.collectionCellModel didClickSelf];
}
- (void)setCustomCellModel:(MenuCollectionViewCellModel *)customCellModel{
	[super setCustomCellModel:customCellModel];
	Menu *modelObject = customCellModel.modelObject;
    [self.menuButton setTitle:modelObject.title forState:UIControlStateNormal];
    [self.menuButton setImage:modelObject.icon forState:UIControlStateNormal];
}
- (CGSize)sizeThatFits:(CGSize)size{
    CGSize s = [self.menuButton sizeThatFits:size];
    CGSize sizeFits = [MKCGRect scaleSize:s aspectFitToMaxSize:CGSizeMake(100, 100)];
    return sizeFits;
}
#pragma mark - protocol:MKUICollectionViewCellProtocol
MKDEF_SINGLETON(MenuCollectionViewCell)//动态尺寸
+ (CGSize)sizeWithCollectionView:(UICollectionView *)collectionView collectionCellModel:(MKUICollectionViewCellModel *)collectionCellModel{
//    NSIndexPath *p = collectionCellModel.indexPathInModel;
//    if(p.item%3!=0){
//        return CGSizeMake(20, 20);
//    }
	return [self dynamicSizeWithCollectionView:collectionView collectionCellModel:collectionCellModel cellShareInstance:[self sharedInstance] calBlock:^CGSize(UICollectionView *collectionView, MKUICollectionViewCellModel *cellModel, MenuCollectionViewCell *cell) {
//        CGSize size = [cell.menuButton mk_sizeThatFits:cell.bounds.size limitInSize:CGSizeMake(100, 60)];
        CGSize size = [cell sizeThatFits:cell.bounds.size];
		return size;
	}];
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCellModel:(MenuCollectionViewCellModel *)cellModel{
    Menu *modelObject = cellModel.modelObject;
    if(!modelObject.icon&&modelObject.iconUrl.length!=0){//异步加载图片
        @MK_WEAKIFY(self);
        @MK_WEAKIFY(cellModel);
        [modelObject getIconWithCompletion:^(UIImage * _Nonnull image, NSError * _Nonnull error) {
            @MK_NORMALIZE(self);
            @MK_NORMALIZE(cellModel);
            if(image&&self.customCellModel==cellModel){//异步完成之后，需要判断是否还是上一个单元格
                [self.customCellModel refresh];
            }
        }];
    }
}
@end
@implementation MenuCollectionViewCell2
MKDEF_SINGLETON(MenuCollectionViewCell2)//动态尺寸
+ (CGSize)sizeWithCollectionView:(UICollectionView *)collectionView collectionCellModel:(MKUICollectionViewCellModel *)collectionCellModel{
    UICollectionViewFlowLayout *flowlayout = collectionView.mk_collectionViewFlowLayout;
    
    return [self dynamicSizeWithCollectionView:collectionView collectionCellModel:collectionCellModel cellShareInstance:[self sharedInstance] calBlock:^CGSize(UICollectionView *collectionView, MKUICollectionViewCellModel *cellModel, MenuCollectionViewCell *cell) {
        CGSize size = [cell.menuButton sizeThatFits:cell.bounds.size];
//        CGSize size = [cell.menuButton mk_sizeThatFits:cell.bounds.size limitInSize:CGSizeMake(100, 60)];
        
        if(flowlayout){
            NSInteger sectionIndex = collectionCellModel.indexInSectionModel;
            if(flowlayout.scrollDirection==UICollectionViewScrollDirectionVertical){
                size.width = [flowlayout mk_contentBoundsForSectionAtIndex:sectionIndex].size.width;
            }else{
                size.height = [flowlayout mk_contentBoundsForSectionAtIndex:sectionIndex].size.height;
            }
        }
        return size;
    }];
}
@end


@implementation MenuCollectionViewCell3
- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.menuButton.titleLabel.numberOfLines = 0;
    }
    return self;
}
MKDEF_SINGLETON(MenuCollectionViewCell3)//动态尺寸
+ (CGSize)sizeWithCollectionView:(UICollectionView *)collectionView collectionCellModel:(MKUICollectionViewCellModel *)collectionCellModel{
    UICollectionViewFlowLayout *flowlayout = collectionView.mk_collectionViewFlowLayout;
    return [self dynamicSizeWithCollectionView:collectionView collectionCellModel:collectionCellModel cellShareInstance:[self sharedInstance] calBlock:^CGSize(UICollectionView *collectionView, MKUICollectionViewCellModel *cellModel, MenuCollectionViewCell *cell) {
        CGSize boundsSize = collectionView.bounds.size;
        if(flowlayout){
            boundsSize = [flowlayout mk_contentBoundsForSectionAtIndex:cellModel.indexInSectionModel].size;
            //分页
            if(flowlayout.scrollDirection==UICollectionViewScrollDirectionVertical){
                boundsSize.width = 99999999;
            }else{
                boundsSize.height = 99999999;
            }
        }
        CGSize size = [cell.menuButton sizeThatFits:boundsSize];
        if(flowlayout){
            NSInteger sectionIndex = collectionCellModel.indexInSectionModel;
            //分页
            if(flowlayout.scrollDirection==UICollectionViewScrollDirectionVertical){
                size.height = [flowlayout mk_contentBoundsForSectionAtIndex:sectionIndex].size.height;
            }else{
                size.width = [flowlayout mk_contentBoundsForSectionAtIndex:sectionIndex].size.width;
            }
        }
        return size;
    }];
}
@end

@implementation MenuCollectionViewCell4
- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.paingLine = [[UIView alloc] init];
        self.paingLine.backgroundColor = [UIColor systemBlueColor];
        [self.contentView addSubview:self.paingLine];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.contentView.bounds;
    CGRect f1 = bounds;
    f1.size.width = 1;
    f1.origin.x = self.mk_collectionViewOfFirst.mk_collectionViewPageFlowLayout.pagingCellPosition*bounds.size.width;
    self.paingLine.frame = f1;
}
@end

@implementation MenuCollectionViewCell_FixSize
+ (CGSize)sizeWithCollectionView:(UICollectionView *)collectionView collectionCellModel:(MKUICollectionViewCellModel *)collectionCellModel{
    return CGSizeMake(60, 60);
}

@end
